﻿using Infrastructure.Dao;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DormMS.IService;
using DormMS.Model.SearchModels;
using DormMS.Model.SearchModels.DropGroup;
using DormMS.Model.ViewModels;
using DormMS.Model;

namespace DormMS.Service
{
    public class GroupHeadService : IGroupHeadService
    {
        private SqlSugarClient db;

        public IEnumerable<V_GroupHead> GetGroupHeadList(int skip, int take, int dropgroupid, ref int total)
        {
            using (db = DbConfig.GetInstance())
            {
                db.CurrentFilterKey = "";

                var list = db.Queryable<Model.GroupHead>().JoinTable<Product_Spec>((m, p1) => m.SpecificationsID == p1.Id).JoinTable<GroupHead, Product_Spec, Product>((m, p1, p) => p1.ProductSku == p.Sku).Where("getdate()< DATEADD(hour,cast(DropGroupTime as int),CreateTime) and DropGroupID='" + dropgroupid + "' and p.isdeleted=0 and p1.isdeleted=0 and p.IsDisplay=1")
                    .OrderBy(m => m.CreateTime, OrderByType.Desc).Select("m.*")
                    .ToPageList(skip / take + 1, take, ref total)
                    .Select(s => new V_GroupHead()
                    {
                        CreateTime = s.CreateTime,
                        DropGroupID = s.DropGroupID,
                        DropGroupTime = s.DropGroupTime,
                        DropPrice = s.DropPrice,
                        EndTime = s.CreateTime.AddHours(s.DropGroupTime),
                        FloorPrice = s.FloorPrice,
                        GropGroupTitle = db.Queryable<Model.DropGroup>().Where(t => t.DropGroupID == s.DropGroupID).FirstOrDefault().Title,
                        GroupHeadID = s.GroupHeadID,
                        InvolvedNum = s.InvolvedNum,
                        IsDeleted = s.IsDeleted,
                        NowPrice = s.NowPrice,
                        OrderID = s.OrderID,
                        OriginalPrice = s.OriginalPrice,
                        Members = db.Queryable<Model.GroupMember>().Where(t => t.GroupHeadID == s.GroupHeadID).ToList(),
                        PayPrice = s.PayPrice,
                        Phone = s.Phone,
                        SpecificationsID = s.SpecificationsID,
                        ProductImgUrl = db.Queryable<Model.Product>().Where("sku = (select top 1  productsku from Product_Spec where Id=" + s.SpecificationsID + ") ").FirstOrDefault().ImgUrl,
                        RebatePrice = s.RebatePrice,
                        StartingPrice = s.StartingPrice,
                        UserHeadImg = s.UserHeadImg,
                        UserID = s.UserID,
                        UserName = s.UserName
                    }).ToList();
                return list;
            }
        }


        public V_GroupHead GetGroupHeadInfo(int groupheadid, ref int total)
        {
            using (db = DbConfig.GetInstance())
            {
                db.CurrentFilterKey = "";
                var model = db.Queryable<Model.GroupHead>().JoinTable<Product_Spec>((m, p1) => m.SpecificationsID == p1.Id).JoinTable<GroupHead, Product_Spec, Product>((m, p1, p) => p1.ProductSku == p.Sku).Where(" p.isdeleted=0 and p1.isdeleted=0 and p.IsDisplay=1")
                    .OrderBy(m => m.CreateTime, OrderByType.Desc).Where(m => m.GroupHeadID == groupheadid).Select("m.*")
                    .ToPageList(0 / 5 + 1, 5, ref total)
                    .Select(s => new V_GroupHead()
                    {
                        CreateTime = s.CreateTime,
                        DropGroupID = s.DropGroupID,
                        DropGroupTime = s.DropGroupTime,
                        DropPrice = s.DropPrice,
                        EndTime = s.CreateTime.AddHours(s.DropGroupTime),
                        FloorPrice = s.FloorPrice,
                        GropGroupTitle = db.Queryable<Model.DropGroup>().Where(t => t.DropGroupID == s.DropGroupID).FirstOrDefault().Title,
                        GroupHeadID = s.GroupHeadID,
                        InvolvedNum = s.InvolvedNum,
                        IsDeleted = s.IsDeleted,
                        NowPrice = s.NowPrice,
                        OrderID = s.OrderID,
                        OriginalPrice = s.OriginalPrice,
                        Members = db.Queryable<Model.GroupMember>().Where(t => t.GroupHeadID == s.GroupHeadID).ToList(),
                        PayPrice = s.PayPrice,
                        Phone = s.Phone,
                        SpecificationsID = s.SpecificationsID,
                        ProductImgUrl = db.Queryable<Model.Product>().Where("sku = (select top 1  productsku from Product_Spec where Id=" + s.SpecificationsID + ")").FirstOrDefault().ImgUrl,
                        RebatePrice = s.RebatePrice,
                        StartingPrice = s.StartingPrice,
                        UserHeadImg = s.UserHeadImg,
                        UserID = s.UserID,
                        UserName = s.UserName
                    }).ToList();
                if (model.Count > 0)
                {
                    return model[0];
                }
                else
                {
                    return null;
                }
            }
        }

      

        /// <summary>
        /// 分页获取活动列表
        /// </summary>
        /// <param name="search"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public IEnumerable<V_GroupHead> GetDropHeadList(DropGroupSearchModel search, ref int total)
        {
            using (db = DbConfig.GetInstance())
            {
                db.CurrentFilterKey = "FalseDelte";
                search.OrderByFields = search.OrderByFields;

                var list = db.Queryable<Model.GroupHead>()
                    .Where(m => m.UserName.Contains(search.Keywords) && m.DropGroupID == search.DropGroupID)
                    .OrderBy(search.OrderByFields).ToPageList(search.Skip / search.Take + 1, search.Take, ref total)
                    .Select(s => new V_GroupHead()
                    {
                        CreateTime = s.CreateTime,
                        DropGroupID = s.DropGroupID,
                        DropGroupTime = s.DropGroupTime,
                        DropPrice = s.DropPrice,
                        EndTime = s.CreateTime.AddHours(s.DropGroupTime),
                        FloorPrice = s.FloorPrice,
                        GroupHeadID = s.GroupHeadID,
                        InvolvedNum = s.InvolvedNum,
                        IsDeleted = s.IsDeleted,
                        NowPrice = s.NowPrice,
                        OrderID = s.OrderID,
                        OriginalPrice = s.OriginalPrice,
                        Phone = s.Phone,
                        PayPrice = s.PayPrice,
                        RebatePrice = s.RebatePrice,
                        SpecificationsID = s.SpecificationsID,
                        StartingPrice = s.StartingPrice,
                        UserHeadImg = s.UserHeadImg,
                        UserID = s.UserID,
                        UserName = s.UserName
                    }).ToList();
                return list;
            }
        }

        /// <summary>
        /// 分页获取活动列表
        /// </summary>
        /// <param name="search"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public IEnumerable<GroupMember> GetDropMemberList(DropGroupSearchModel search, ref int total)
        {
            using (db = DbConfig.GetInstance())
            {
                db.CurrentFilterKey = "FalseDelte";
                search.OrderByFields = search.OrderByFields;

                var list = db.Queryable<Model.GroupMember>()
                    .Where(m => m.UserName.Contains(search.Keywords) && m.GroupHeadID == search.DropGroupID)
                    .OrderBy(search.OrderByFields).ToPageList(search.Skip / search.Take + 1, search.Take, ref total).ToList();
                return list;
            }
        }
        /// <summary>
        /// 根据订单号获取团信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Model.GroupMember GetGroupMemberInfo(string orderno)
        {
            using (db = DbConfig.GetInstance())
            {
                var model = db.Queryable<Model.GroupMember>().SingleOrDefault(t => t.OrderNo == orderno);
                return model;
            }
        }

        /// <summary>
        /// 根据团Id获取团信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Model.GroupHead GetGroupHeadInfoByID(int Id)
        {
            using (db = DbConfig.GetInstance())
            {
                var model = db.Queryable<Model.GroupHead>().SingleOrDefault(t => t.GroupHeadID == Id);
                return model;
            }
        }
    }
}
